
const { cookie } = require('request');
var request = require('request');
var format = require('string-format')


let cookie = "WEB_USERLOGIN_USERNAME_COOKIE=212113412100006; WEB_USERLOGIN_USERPWD_COOKIE=eHNkMTIzNDU2; sspm_proctorUrl=http://wljy.whut.edu.cn:80/; sspm_orgid=4406; sspm_appid=157438568781088; JSESSIONID=70EDD9D9AD39495D68EB2CB562463B8D"

// http://wljy.whut.edu.cn/web/kpointdetail.htm?kpointNo=9583&courlibNo=1963&courseNo=75
// pkVal = 9583 // kpointNo 
// courNo =1963 // courlibNo
var argsArr = [
    //[pkVal, courNo]
    [100158, 1973],
    [76854,1974],
    [100004,2150],
    [9661,1963],
    [101308,1765]
];




let url = "http://wljy.whut.edu.cn/web/submitCommentCalScore.ajax" ;

// c语言 ,                  100158, 1973
// 线性代数与概率统计        76854,1974
//形势与政策                100004,2150
// 大学英语                 9661,1963
// 网络教育学习导论(新)	     101308,1765
var requestData1="tableName=EDU_COURSE_KPOINT&pkVal={}&content=%E8%80%81%E5%B8%88%E8%BE%9B%E8%8B%A6%E4%BA%86&commitId=-1&score=5&noName=0&timeLimit=1&courNo={}";

for (let i = 0; i < argsArr.length; i++) {
    args = argsArr[i]
    let requestData = format(requestData1,args[0], args[1])
    console.log(requestData);
}


const sleep = function (ms){
    return new Promise(resolve => setTimeout(resolve, ms))
}
const send = async function(){

    for (let i = 0; i < argsArr.length; i++) {
        console.log("=========================开始请求 %s=========================", i);

        args = argsArr[i]
        let requestData = format(requestData1,args[0], args[1])
        console.log(requestData);

        let time = random(5000,15000)
        httprequest2(url, requestData)
        await sleep(time)
    }
  }
  
send()

function random(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

function random2(min,max){
    let n = Math.random()*(max-min+1)+min;
    if(n>30){
        return 30 ;
    }
    return n;
}


// let loginUrl = "http://wljy.whut.edu.cn/web/login/checkLogin.ajax"
// res = httprequest(loginUrl, "username=212113412100006&password=eHNkMTIzNDU2&savePwd=off");

// function httprequest(url, data){
//     console.log("url :", url) // 请求成功的处理逻辑
//     request({
//         url: url,
//         method: "POST",
//         json: false,
//         headers: {
//             "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
//         },
//         body: data
//     }, function(error, response, body) {
//         console.log("code:", response.statusCode) // 请求成功的处理逻辑
//         console.log("cookie:",response.headers['set-cookie']);
//         console.log("body:", response) 
//         cie = response.headers['set-cookie']
//         console.log("登陆完成...");
//         if (!error && response.statusCode == 200) {

//         }
//     });
// };


function httprequest2(url, data){
    // console.log("url :", url) // 请求成功的处理逻辑
    request({
        url: url,
        method: "POST",
        json: false,
        headers: {
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
            Cookie: cookie, //这里是登陆后得到的cookie,(重点)
        },
        body: data
    }, function(error, response, body) {
        try {
            console.log("body:", body) 
        }catch(e) {
        }

    });
};





 
 